package com.nf.health.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.MyGridView;
import com.nf.health.app.models.HealthSchemeVo;
import com.nf.health.app.models.MapVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 健康详情适配器
 * Created by Administrator on 2016/3/23.
 */
public class HealthSchemeAdapter extends BaseAdapter{


    ViewHolder holder;

    Map<String,String> map = new HashMap<String,String>();

    private int currentPos = -1;


    List<MapVo> mlist = new ArrayList<MapVo>();

    LinearLayout.LayoutParams p;


    LinearLayout lin_yi,lin_wei;


    private List<HealthSchemeVo> accounts;
    private Context context;

    Boolean bb = true;

    GridViewAdapter myAdapter;

    public HealthSchemeAdapter(Context context,List<HealthSchemeVo> accounts) {
        this.context = context;
        this.accounts = accounts;

    }

    @Override
    public int getCount() {
        if(accounts != null){
            return accounts.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        holder = ViewHolder.get(parent, convertView, R.layout.item_health_scheme_item, position);

        TextView health_scheme_name = holder.getView(R.id.health_scheme_name);
        TextView health_scheme_ts = holder.getView(R.id.health_scheme_ts);
        Button today_btn = holder.getView(R.id.today_btn);
        TextView yi_health_scheme_name = holder.getView(R.id.yi_health_scheme_name);//展开后的健康计划名
        LinearLayout lin_dier = holder.getView(R.id.lin_dier);//动态添加布局
        LinearLayout lin_disan = holder.getView(R.id.lin_disan);//点击展开按钮
        LinearLayout lin_disan2 = holder.getView(R.id.lin_disan2);//点击收起按钮
        lin_yi = holder.getView(R.id.lin_yi);//点击展开按钮显示的布局
        lin_wei = holder.getView(R.id.lin_wei);//点击展开按钮隐藏的布局

        MyGridView health_scheme_gridview = holder.getView(R.id.health_scheme_gridview);//Gridview



        map =  accounts.get(position).getSchedule();


        for (String key : map.keySet()) {
            MapVo mapVo = new MapVo(key,map.get(key));
            mlist.add(mapVo);
        }


        myAdapter= new GridViewAdapter(mlist,context);
                    health_scheme_gridview.setAdapter(myAdapter);


        health_scheme_name.setText(accounts.get(position).getName());//名称赋值

                int a = Integer.parseInt(accounts.get(position).getDaynum());
                int b = Integer.parseInt(accounts.get(position).getTotaldaynum());

                final int c = a*100/b;

                health_scheme_ts.setText(c + "%");
//






        lin_disan2.setTag(position);
        lin_disan.setTag(position);
        lin_disan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                int pos = (Integer) v.getTag();
                currentPos = pos;
                notifyDataSetChanged();
            }
        });

        lin_disan2.setOnClickListener(new View.OnClickListener() {//收起按钮

            @Override
            public void onClick(View v) {
                int pos = (Integer) v.getTag();
                currentPos = pos;
                notifyDataSetChanged();
            }
        });
//        点击展开 点击隐藏
        if (currentPos == position) {
            if (lin_yi.getVisibility() == View.VISIBLE) {
                lin_wei.setVisibility(View.VISIBLE);
                lin_yi.setVisibility(View.GONE);
            } else {
                lin_yi.setVisibility(View.VISIBLE);
                lin_wei.setVisibility(View.GONE);
            }
        }

        today_btn.setOnClickListener(new View.OnClickListener() {//今日完成按钮
            @Override
            public void onClick(View v) {



            }
        });

        return holder.getConvertView();
    }


}
